<?php

/**
 * 管理基类
 * 
 * @author 叶风
 * Qq:    355007778
 * Email: ye.fn@163.com
 */

namespace c\shop\order;

class b extends g
{
	var $class = array('GG'=>'\c\goods\g');
	
    protected function _home($w='')
    {
		$this->title = \lang::g('Shop Order');
        $s = $this->P_G('s');
        $t = array('t1'=> array('as'=>$this->table));
        $field = 't1.*';
        if ($w=='')
         {
            $t['ui'] = array('as'=>'user' , 'join'=>'LEFT JOIN', 'on'=>'ui.id=t1.user_id');
            $field .= ',ui.name userName';
         }
        
        if ($s['name'])
         {
            $t['d1'] = array('as'=>'shop_detail', 'join'=>'JOIN', 'on'=>'d1.order_id=t1.id');
            $name = $this->db->escape($s['name']);
            $w .= " AND (t1.sn ='$name' or d1.name LIKE '%$name%')";
         }
        if ($s['state']!='')
            $w .= ' AND t1.state='. intval($s['state']);
        if ($s['timeS']!='')
            $w .= ' AND DATE_FORMAT(t1.time, "%Y-%m-%d")>="'. $this->db->escape($s['timeS']) .'"';
        if ($s['timeE']!='')
            $w .= ' AND DATE_FORMAT(t1.time, "%Y-%m-%d")<="'. $this->db->escape($s['timeE']) .'"';
         
        if ($w)
            $w = substr($w, 4);
        $this->t_var['res'] = $res = $this->db->get_page(array(), $t, $field, $w, 't1.id DESC');
        
        
        //detail
        if ($res){
            $ids='';
            foreach ($res as $v)
                $ids .= ','. $v['id'];
            $ids = substr($ids, 1);
            $_r = $this->db->get('shop_detail', '*', "order_id IN($ids)");
            $detailR = array();
            foreach ($_r as $v)
                $detailR[$v['order_id']][] = $v;
            
            $this->t_var['detailR'] = $detailR;
         }
         
        $this->t_var['s'] = $s;
        $this->t_var['state'] = \lang::Ag_p($this->State, 'ord_state');
        $this->loadTemplate('shop/order/list');
    }
    protected function _view($ord)
    {
        $this->t_var['ord'] = $ord;
        $this->t_var['detailR'] = $this->db->get('shop_detail', '*', 'order_id='. $ord['id']);
        $this->t_var['flowR'] = $this->db->get('shop_flow', '*', 'order_id='. $ord['id'], 'id DESC');
        $this->t_var['state'] = \lang::Ag_p($this->State, 'ord_state');
        $this->loadTemplate('shop/order/view');
    }
    protected function _del($w)
    {
        $id = intval($_GET['id']);
        $t =array('t1'=> array('as'=>$this->table));
        $t['d1'] = array('as'=>'shop_detail', 'join'=>'LEFT JOIN', 'on'=>'d1.order_id=t1.id');
        $t['d2'] = array('as'=>'shop_flow', 'join'=>'LEFT JOIN', 'on'=>'d2.order_id=t1.id');
        $this->db->del($t, 't1.id='. $id . $w);
        if ($this->db->num_affected()>0){
            \log::write($this->table, 'del', $id);
            ajax_json(TRUE);
         }
        ajax_json(\lang::g('Over').$w);
    }
	
}
